package argo.hot100;

import java.util.HashSet;
import java.util.Set;

public class LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {

        if (s.length() <= 1) {
            return s.length();
        }

        int max = 0, cur = 1, left = 0, right = left + 1;
        Set<Character> set = new HashSet<>();
        set.add(s.charAt(left));
        while (right < s.length()) {
            while (set.contains(s.charAt(right))) {
                set.remove(s.charAt(left));
                left++;
            }
            set.add(s.charAt(right));
            cur = set.size();
            max = Math.max(cur, max);
            right++;
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(new LengthOfLongestSubstring().lengthOfLongestSubstring("dvdf"));

        System.out.println(new LengthOfLongestSubstring().lengthOfLongestSubstring("abcdabcbb"));
        System.out.println(new LengthOfLongestSubstring().lengthOfLongestSubstring("bbbb"));
        System.out.println(new LengthOfLongestSubstring().lengthOfLongestSubstring("pwwkew"));
    }

}
